<?php

use Illuminate\Database\Seeder;
use App\Model\Admin\User;
use Faker\Factory;

class AdminUserTableSeeder extends Seeder
{
    /**
     * php artisan make:seeder AdminUserTableSeeder
     *
     * php artisan db:seed --class=AdminUserTableSeeder
     *
     * Run the database seeds.
     *
     * @return void
     */

    public function run()
    {
        $users = $this->getData();

        $valid =0;
        $exists = 0;
        foreach ($users as $user) {
            $addition = [
                'is_active' => rand(0, 1),
                'is_locked' => rand(0, 1),
            ];
            $user = array_merge($user, $addition);

            $exist = User::where('email', $user['email'])->first();

            if (!$exist || !$exist->id) {
                User::firstOrCreate($user);
                $valid++;
            } else {
                $exists++;
            }
        }

        echo "";
    }

    public function getData()
    {
        $data = [
            [
                'name' => 'admin',
                'email' => 'admin@admin.com',
                'password' => bcrypt(123456),
            ],
            [
                'name' => 'root',
                'email' => 'root@root.com',
                'password' => bcrypt(123456),
            ],
        ];

        $i = 0;

        $faker = Factory::create('zh');
        do {
            $data[] = [
                'email' => $faker->email,
                'password' => bcrypt(123456),
            ];

            $i++;
        } while ($i < 48);

        foreach ($data as &$item) {
            $item['name'] = preg_replace('/@.*/', '',$item['email']);
            $item['is_active'] = rand(0, 1);
        }

        return $data;
    }
}
